package com.ict.web.controller.yzq;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.ict.common.core.page.TableDataInfo;
import com.ict.system.dto.BaseResponse;
import com.ict.system.dto.FileUploadResp;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.domain.AnnualProjectPlan;
import com.ict.web.domain.ProjectManagement;
import com.ict.web.domain.ReviewTask;
import com.ict.web.dto.AnnualProjectPlanAddDto;
import com.ict.web.dto.AnnualProjectPlanEditDto;
import com.ict.web.dto.AnnualProjectPlanListDto;
import com.ict.web.dto.SelectPlanDto;
import com.ict.web.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.ict.common.annotation.Log;
import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.enums.BusinessType;
import com.ict.web.service.IAnnualProjectPlanService;
import com.ict.common.utils.poi.ExcelUtil;

import org.springframework.web.multipart.MultipartFile;

/**
 * 年度项目计划Controller
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Api(tags = "年度项目计划")
@RestController
@RequestMapping("/plan")
public class AnnualProjectPlanController extends BaseController {
    @Autowired
    private IAnnualProjectPlanService annualProjectPlanService;

    /**
     * 查询年度项目计划列表
     */
//    @PreAuthorize("@ss.hasPermi('web:plan:list')")
    @ApiOperation("查询年度项目计划列表")
    @GetMapping("/list")
    public TableDataInfo list(AnnualProjectPlanListDto dto) {
        startPage();
        return annualProjectPlanService.selectAnnualProjectPlanList(dto);
    }


    /**
     *增补转年度
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('web:plan:auditList')")
    @ApiOperation("增补转年度")
    @GetMapping("/supplement2year/{planId}/{proId}")
    public AjaxResult auditList(@PathVariable("planId")Long planId,@PathVariable("proId")Long proId) {
        return annualProjectPlanService.supplement2year(planId,proId);
    }

    /**
     * 获取年度项目计划详细信息
     */
//    @PreAuthorize("@ss.hasPermi('web:plan:query')")
    @ApiOperation("获取年度项目计划详细信息")
    @GetMapping(value = "/{planId}")
    public AnnualProjectPlanDetailsVo getInfo(@PathVariable("planId") Long planId) {
        return annualProjectPlanService.selectAnnualProjectPlanByPlanId(planId);
    }

    /**
     * 查询年度计划对应的项目
     *
     * @param selectPlanDto 年度项目计划
     * @return 结果
     */
    @ApiOperation("查询年度计划对应的项目")
    @GetMapping(value = "/selectPlanNumber")
    public PageInfo<ProjectManagementListVo> selectPlanNumber(SelectPlanDto selectPlanDto) {
        return annualProjectPlanService.selectPlanNumberPage(selectPlanDto);
    }

    /**
     * 导出年度项目计划--李涛列表
     */
    @ApiOperation("导出年度项目计划")
    @Log(title = "年度项目计划--李涛", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long planId) throws UnsupportedEncodingException {
        List<ProjectManagementListVo> list = annualProjectPlanService.selectPlanNumber(planId);
        List<ProjectManagementListExcel> listExcels = new ArrayList<>();

        list.forEach(s -> {
            ProjectManagementListExcel projectManagementListExcel = new ProjectManagementListExcel();
            BeanUtils.copyProperties(s, projectManagementListExcel);
            String investmentEstimationS =
                    projectManagementListExcel.getInvestmentEstimation().stripTrailingZeros().toPlainString();
            projectManagementListExcel.setInvestmentEstimationS(investmentEstimationS);
            String type = new String();
            switch (projectManagementListExcel.getType()) {
                case "0":
                    type = "建设类(新开工)";
                    break;
                case "1":
                    type = "建设类(续建)";
                    break;
                case "2":
                    type = "购买服务类";
                    break;
                case "3":
                    type = "运维类";
                    break;
                case "4":
                    type = "其他类";
                    break;
            }
            projectManagementListExcel.setType(type);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = s.getCreateTime().format(dateTimeFormatter);
            projectManagementListExcel.setCreateTime(format);
            listExcels.add(projectManagementListExcel);
        });
        ExcelUtil<ProjectManagementListExcel> util = new ExcelUtil<>(ProjectManagementListExcel.class);
        //response输出文件流
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("年度项目列表导出.xlsx", "UTF-8"));
        util.exportExcel(response, listExcels, "年度项目计划--数据");
    }

    /**
     * 新增年度项目计划
     */
    @PreAuthorize("@ss.hasPermi('web:plan:add')")
    @ApiOperation("新增年度项目计划")
    @Log(title = "年度项目计划", businessType = BusinessType.INSERT)
    @PostMapping
    public Long add(@RequestBody AnnualProjectPlanAddDto dto) {
        return annualProjectPlanService.insertAnnualProjectPlan(dto);
    }

    @PreAuthorize("@ss.hasPermi('web:plan:updateStag')")
    @ApiOperation("修改状态值，正在申报变暂停，暂停变正在申报")
    @Log(title = "修改状态值", businessType = BusinessType.INSERT)
    @PostMapping("/updateStag/{planId}")
    public AjaxResult updateStag(@PathVariable Long planId) {
        return toAjax(annualProjectPlanService.updateStag(planId));
    }

    /**
     * 修改年度项目计划
     */
    @PreAuthorize("@ss.hasPermi('web:plan:edit')")
    @ApiOperation("修改年度项目计划")
    @Log(title = "年度项目计划", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/{planId}")
    public AjaxResult edit(@RequestBody AnnualProjectPlanEditDto dto, @PathVariable Long planId) {
        return toAjax(annualProjectPlanService.updateAnnualProjectPlan(dto, planId));
    }

    /**
     * 删除年度项目计划
     */
    @PreAuthorize("@ss.hasPermi('web:plan:remove')")
    @Log(title = "年度项目计划", businessType = BusinessType.DELETE)
    @ApiOperation("删除年度项目计划")
    @DeleteMapping("/{planId}")
    public AjaxResult remove(@PathVariable Long planId) {
        return toAjax(annualProjectPlanService.deleteAnnualProjectPlanByPlanId(planId));
    }

    /**
     * 查询当前是否有年度计划
     *
     * @return 结果
     */
    @ApiOperation("查询当前是否有年度计划")
    @GetMapping(value = "/ifAnnualProject")
    public AjaxResult ifAnnualProject() {
        return AjaxResult.success().put("annualProjectPlan", annualProjectPlanService.ifAnnualProject());
    }

    /**
     * 获取年度计划年份
     *
     * @return 结果
     */
    @ApiOperation("获取年度计划年份")
    @GetMapping(value = "/getYear")
    public AjaxResult getYear() {
        return AjaxResult.success().put("year", annualProjectPlanService.getYear());
    }

    /**
     * 获取年度计划列表开始截至时间
     *
     * @return 结果
     */
    @ApiOperation("获取年度计划列表开始截至时间")
    @GetMapping(value = "/getTimeList")
    public AjaxResult getTimeList() {
        return AjaxResult.success().put("timeList", annualProjectPlanService.getTimeList());
    }

    /**
     * 上传专家审核结论
     */
    @PreAuthorize("@ss.hasPermi('web:plan:upload')")
    @ApiOperation(value = "上传专家审核结论", httpMethod = "POST")
    @RequestMapping(value = "/upload/{planId}", method = RequestMethod.POST)
    public AjaxResult upload(@Validated @RequestBody YzqFile file, @PathVariable Long planId) {
        return annualProjectPlanService.upload(file, planId);
    }

    /**
     * 删除上传专家审核结论
     *
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('web:plan:deleteFile')")
    @ApiOperation("删除上传专家审核结论")
    @DeleteMapping(value = "/deleteFile")
    public AjaxResult deleteFile(Long fileId) {
        return toAjax(annualProjectPlanService.deleteFile(fileId));
    }

    @PreAuthorize("@ss.hasPermi('web:plan:terminateAnnualPlan')")
    @ApiOperation("终止年度计划")
    @GetMapping(value = "/terminateAnnualPlan/{planId}")
    public AjaxResult terminateAnnualPlan(@PathVariable Long planId){
        return toAjax(annualProjectPlanService.terminateAnnualPlan(planId));
    }
}
